package com.yulintu.parser;

import com.yulintu.common.FileBodyResult;
import com.yulintu.dao.BaseBodyDaoService;
import com.yulintu.header.Headr;

import java.util.List;
import java.util.Optional;

/**
 *
 * 文件头解析器
 *
 * Run方法，处理并解析data数据，将结果封装到BodyResult中去
 * @author caomin
 * @date 2017/10/25
 * @说明
 */
public abstract class AbstractBodyParser<T> implements Runnable{

    public AbstractBodyParser (FileBodyResult<T> fileBodyResult){
        this.fileBodyResult=fileBodyResult;
    }

    protected FileBodyResult<T> fileBodyResult =new FileBodyResult<T>();


    protected T data;

    /**
     * 根据key获取header
     * @param key
     * @return
     */
    public  Headr getHeader(String key){
        List<Headr> fileBodyResultHeadrs = fileBodyResult.getHeadrs();
        Optional<Headr> first = fileBodyResultHeadrs.stream().filter(hear -> key.equalsIgnoreCase(hear.getKey())).findFirst();
        return first.get();
    }

    /**
     * 获取所有header
     * @return
     */
    public    List<Headr> getHeaders(){
        return  this.fileBodyResult.getHeadrs();
    }

    /**
     * 通过解析头，获取相应 的处理dao
     * @return
     */
    public abstract BaseBodyDaoService getBaseBodyDaoService(List<Headr> headrs);

    /**
     *
     * @return
     */
    public abstract BaseBodyDaoService getBaseBodyDaoService();

    /**
     *获取具体的body处理器
     * @return
     */
    public abstract AbstractBodyParser<T> getAbstractBodyParser();


    /**
     * 通过解析传进来的data,获取对应 的BodyResult
     * @return
     */
    public FileBodyResult<T> getFileBodyResult() {
        return fileBodyResult;
    }



    public AbstractBodyParser setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public void run() {
        System.out.println("解析数据，并把结果放到bodyResult");
        this.fileBodyResult =new FileBodyResult<T>();
    }

    public void setFileBodyResult(FileBodyResult<T> fileBodyResult) {
        this.fileBodyResult = fileBodyResult;
    }
    public AbstractBodyParser(){}
}
